package com.khaled.protclass.featureselection.main;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.khaled.protclass.featureselection.configuration.Configuration;
import com.khaled.protclass.model.base.IFeature;
import com.khaled.protclass.model.base.IProtein;
import com.khaled.protclass.model.bean.weka.WekaFile;
import com.khaled.protclass.model.loader.ProteinLoader;

public class FeatureSelection
{
	/** Logger. */
	private static Logger logger = Logger.getLogger(FeatureSelection.class);

	private static final String CLASS_1 = "0";

	private static final String CLASS_2 = "1";

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		logger.info("Protein To Features Application Starts.");

		try
		{
			Configuration config = Configuration.getInstance();
			WekaFile wekaFile = new WekaFile(config.getFeatures(), config.getInput().getRelation_name());

			// 1. Extract features from proteins list
			ProteinLoader loader = ProteinLoader.getInstance(config.getInput().getProteinType());

			ArrayList<IProtein> proteinList = new ArrayList<IProtein>();

			ArrayList<IProtein> proteins_class_1 = loader.loadProteins(config.getInput().getClass1_path(), CLASS_1);
			ArrayList<IProtein> proteins_class_2 = loader.loadProteins(config.getInput().getClass2_path(), CLASS_2);

			proteinList.addAll(proteins_class_1);
			proteinList.addAll(proteins_class_2);

			int index = 0;
			for (IProtein protein : proteinList)
			{
				protein.setProteinIndex(index++);

				StringBuffer proteinFeatureVector = new StringBuffer();

				// 1.1 Iterate over all features and get feature vector of each
				// one
				for (IFeature feature : config.getFeatures())
				{
					proteinFeatureVector.append(feature.getFeatureVector(protein));
				}
				// 1.2 add the class attribute at the end of the feature vector
				proteinFeatureVector.append(protein.getClassification());

				// 1.3 add the feature vector to the list of data in the weka
				// file
				wekaFile.addFeatureVector(proteinFeatureVector.toString());
			}

			// 2. Save weka file in the output path
			wekaFile.save(config.getInput().getOutputFilePath());
		}
		catch (IOException e)
		{
			logger.error(e.getClass());
			logger.error(e.getCause());
			logger.error(e.getMessage());

			e.printStackTrace();
		}

	}
}
